package cc.vv.baselibrary.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.List;

import cc.vv.baselibrary.R;
import cc.vv.baselibrary.view.pickerview.adapter.ArrayWheelAdapter;
import cc.vv.baselibrary.view.pickerview.lib.WheelView;
import cc.vv.baselibrary.view.pickerview.listener.OnItemSelectedListener;

/**
 * Created by Zxr on 2018/4/19.
 */
public class SinglePickerView {
    /**
     * PopuWindow
     */
    private PopupWindow popuwindow;
    /**
     * 完成
     */
    private TextView tv_pw_confirm;
    /**
     * 取消
     */
    private TextView tv_pw_cannel;
    /**
     * 单选
     */
    private WheelView wv_pw_single;
    /**
     * 选中文字
     */
    private String singleStr = "";
    /**
     * 选中item
     */
    private int item;

    private static SinglePickerView singlePickerView;
    private static Context mContext;
    private static View mView;

    public static SinglePickerView getInstance(Context context, View v) {
        mContext = context;
        mView = v;
        if (singlePickerView == null) {
            singlePickerView = new SinglePickerView();
        }
        return singlePickerView;
    }

    /**
     * 初始化
     */
    public void init(@NonNull final List<String> list, int defPosition, @NonNull final SinglePickListener listener) {

        // 视图加载
        View view = LayoutInflater.from(mContext).inflate(R.layout.popw_single_picker, null);
        wv_pw_single = view.findViewById(R.id.wv_pw_single);
        tv_pw_confirm = view.findViewById(R.id.tv_pw_confirm);
        tv_pw_cannel = view.findViewById(R.id.tv_pw_cannel);
        //设置长宽度
        popuwindow = new PopupWindow(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        //加载视图
        popuwindow.setContentView(view);
        //设置显示位置
        popuwindow.setAnimationStyle(R.style.pickerview_Anim);
        popuwindow.setOutsideTouchable(true);
        popuwindow.setFocusable(true);
        popuwindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setBackgroundAlpha(0.45f);
        popuwindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                item = 0;
                setBackgroundAlpha(1.0f);
            }
        });
        wv_pw_single.setCyclic(false);

        ArrayWheelAdapter adapter = new ArrayWheelAdapter(list);
        wv_pw_single.setCurrentItem(defPosition);
        wv_pw_single.setAdapter(adapter);

        wv_pw_single.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                System.out.println("数据 == index = " + index);
                singleStr = list.get(index);
                item = index;
            }
        });
        view.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    popuwindow.dismiss();
                    return true;
                }
                return false;
            }
        });
        tv_pw_cannel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popuwindow.dismiss();
            }
        });

        tv_pw_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.singlePicked(item, singleStr);
                popuwindow.dismiss();
            }
        });

        popuwindow.showAtLocation(mView, Gravity.BOTTOM, 0, 0);
    }

    /**
     * 设置背景
     */
    private void setBackgroundAlpha(float alpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        lp.alpha = alpha;
        ((Activity) mContext).getWindow().setAttributes(lp);
    }

    /**
     * 单项选择监听
     */
    public interface SinglePickListener {

        void singlePicked(int item, String str);

    }
}
